/**
 * OrderController.java
 * 
 * Copyright@2015 OVT Inc. All rights reserved. 
 * 
 * 2015年12月21日
 */
package com.ovt.order.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ovt.common.model.JsonDocument;
import com.ovt.order.controller.response.PaymentServiceAPIResult;
import com.ovt.order.dao.contant.OrderState;
import com.ovt.order.dao.vo.Order;
import com.ovt.order.dao.vo.OrderItem;
import com.ovt.order.dao.vo.RefundRequest;
import com.ovt.order.service.OrderService;
import com.ovt.order.service.RefundService;
import com.ovt.order.service.exception.ServiceException;

/**
 * OrderController
 * 
 * @Author brad.zhou
 * @Version 1.0
 * @See
 * @Since [OVT Cloud Platform]/[API] 1.0
 */

@Controller
@RequestMapping("/orders")
public class OrderController
{
    @Autowired
    private OrderService orderService;

    @Autowired
    RefundService refundService;
    
    private static final JsonDocument SUCCESS = new JsonDocument();

    @RequestMapping(method = RequestMethod.POST, value = "/create")
    @ResponseBody
    public JsonDocument createOrder(@RequestParam long userId,
            @RequestParam float totalFee, @RequestParam String remark,
            @RequestParam List<OrderItem> productList) throws ServiceException
    {
        Order order = orderService.createOrder(userId, totalFee, remark,
                productList);

        return new PaymentServiceAPIResult(order);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    @ResponseBody
    public JsonDocument queryOrderList(@RequestParam long userId)
            throws ServiceException
    {
        List<Order> orders = orderService.queryOrderList(userId);

        return new PaymentServiceAPIResult(orders);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/detail")
    @ResponseBody
    public JsonDocument queryOrderInfo(@RequestParam long userId,
            @RequestParam String orderNo) throws ServiceException
    {
        Order order = orderService.queryOrderInfo(userId, orderNo);

        return new PaymentServiceAPIResult(order);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/cancel")
    @ResponseBody
    public JsonDocument cancelOrder(@RequestParam long userId,
            @RequestParam String orderNo) throws ServiceException
    {
        Order order = orderService.cancelOrder(userId, orderNo);

        return new PaymentServiceAPIResult(order);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/delete")
    @ResponseBody
    public JsonDocument deleteOrder(@RequestParam long userId,
            @RequestParam String orderNo) throws ServiceException
    {
        Order order = orderService.deleteOrder(userId, orderNo);

        return new PaymentServiceAPIResult(order);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/applyRefund")
    @ResponseBody
    public JsonDocument applyForRefund(@RequestParam long userId,
            @RequestParam String orderNo, @RequestParam int refundReasonId,
            @RequestParam float refundFee, @RequestParam String refundDesc)
            throws ServiceException
    {
        String result = orderService.applyForRefund(userId, orderNo,
                refundReasonId, refundFee, refundDesc);

        return new PaymentServiceAPIResult(result);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/refundList")
    @ResponseBody
    public JsonDocument queryRefundRequestList() throws ServiceException
    {
        List<RefundRequest> refundRequests = orderService
                .queryRefundRequestList();

        return new PaymentServiceAPIResult(refundRequests);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/transferList")
    @ResponseBody
    public JsonDocument queryTransferList()
    {
        return null;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/doAgreeRefund")
    @ResponseBody
    public JsonDocument doAgreeRefundAction(
            @RequestParam List<Long> requestIdList, @RequestParam long auditorId)
            throws ServiceException
    {
        /*
         * 点击退款按钮，根据传进来的idList获取RefundRequest列表,
         * 再针对每个RefundRequest调用refundService.
         */
        List<RefundRequest> refundRequests = orderService
                .queryRefundRequestList(requestIdList);

        refundService.doAliRefund(refundRequests);

//        orderService.setRefundRequestListState(requestIdList,
//                OrderState.REFUND_DOING);
//
//        orderService.putAuditorId(requestIdList, auditorId);
        
        orderService.setRefundRequestList(requestIdList, OrderState.REFUND_DOING, auditorId);

        for (RefundRequest refundRequest : refundRequests)
        {
            refundRequest.setRefundState(OrderState.REFUND_DOING);
        }

        return new PaymentServiceAPIResult(SUCCESS);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/doRejectRefund")
    @ResponseBody
    public JsonDocument doRejectRefundAction(@RequestParam List<Long> requestIdList,
            @RequestParam long auditorId, @RequestBody List<String> feedback) throws ServiceException
    {
//        orderService.setRefundRequestListState(requestIdList,
//                OrderState.REFUND_REFUSED);
        
        orderService.setRefundRequestList(requestIdList, OrderState.REFUND_REFUSED, auditorId, feedback);
        
        return new PaymentServiceAPIResult(SUCCESS);
    }

}
